package com.interest.business.service.impl;

import java.io.IOException;
import java.util.List;

import com.hy.corecode.idgen.WFGIdGenerator;
import com.interest.business.domain.InterestRedeemCode;
import com.interest.business.domain.vo.InterestQueryVo;
import com.interest.business.mapper.InterestRedeemCodeMapper;
import com.interest.common.exception.ServiceException;
import com.interest.common.utils.DateUtils;
import com.interest.common.utils.poi.ExcelUtil;
import com.interest.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.interest.business.mapper.InterestInterestMapper;
import com.interest.business.domain.InterestInterest;
import com.interest.business.service.IInterestInterestService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

/**
 * 权益管理Service业务层处理
 * 
 * @author hzf
 * @date 2024-03-23
 */
@Service
public class InterestInterestServiceImpl implements IInterestInterestService 
{
    @Autowired
    private InterestInterestMapper interestInterestMapper;

    @Autowired
    private InterestRedeemCodeMapper interestRedeemCodeMapper;

    @Autowired
    WFGIdGenerator wfgIdGenerator;

    /**
     * 查询权益管理
     * 
     * @param id 权益管理主键
     * @return 权益管理
     */
    @Override
    public InterestInterest selectInterestInterestById(String id)
    {
        return interestInterestMapper.selectInterestInterestById(id);
    }

    /**
     * 查询权益管理列表
     * 
     * @param interestInterest 权益管理
     * @return 权益管理
     */
    @Override
    public List<InterestInterest> selectInterestInterestList(InterestQueryVo interestInterest)
    {
        return interestInterestMapper.selectInterestInterestList(interestInterest);
    }

    /**
     * 新增权益管理
     *
     * @param file
     * @param interestInterest 权益管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertInterestInterest(MultipartFile file, InterestInterest interestInterest) throws IOException {
        String id = IdUtils.simpleUUID();
        if(file!=null && !file.isEmpty()){
            ExcelUtil<InterestRedeemCode> excelUtil = new ExcelUtil<>(InterestRedeemCode.class);
            List<InterestRedeemCode> codes = excelUtil.importExcel(file.getInputStream());
            if(!codes.isEmpty()){
                codes.forEach(item->{
                    item.setId(IdUtils.simpleUUID());
                    item.setInterestId(id);
                });
                interestRedeemCodeMapper.insertBatch(codes);
            }
        }
        interestInterest.setId(id);
        interestInterest.setCode("QY"+wfgIdGenerator.next());
        interestInterest.setCreateTime(DateUtils.getNowDate());
        return interestInterestMapper.insertInterestInterest(interestInterest);
    }

    /**
     * 修改权益管理
     *
     * @param file
     * @param interestInterest 权益管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateInterestInterest(MultipartFile file, InterestInterest interestInterest) throws IOException {
        String id = interestInterest.getId();
        InterestInterest interest = interestInterestMapper.selectInterestInterestById(id);
        if(interest==null){
            throw new ServiceException("权益不存在:"+interestInterest.getId());
        }
        if(file!=null && !file.isEmpty()){
            ExcelUtil<InterestRedeemCode> excelUtil = new ExcelUtil<>(InterestRedeemCode.class);
            List<InterestRedeemCode> codes = excelUtil.importExcel(file.getInputStream());
            if(!codes.isEmpty()){
                codes.forEach(item->{
                    item.setId(IdUtils.simpleUUID());
                    item.setInterestId(id);
                });
                interestRedeemCodeMapper.insertBatch(codes);
            }
        }
        return interestInterestMapper.updateInterestInterest(interestInterest);
    }

    /**
     * 批量删除权益管理
     * 
     * @param ids 需要删除的权益管理主键
     * @return 结果
     */
    @Override
    public int deleteInterestInterestByIds(String[] ids)
    {
        return interestInterestMapper.deleteInterestInterestByIds(ids);
    }

    /**
     * 删除权益管理信息
     * 
     * @param id 权益管理主键
     * @return 结果
     */
    @Override
    public int deleteInterestInterestById(String id)
    {
        return interestInterestMapper.deleteInterestInterestById(id);
    }

}
